package com.xiaoming.backend.dao.impl;

import com.xiaoming.backend.dao.IOrderDao;
import com.xiaoming.backend.dao.mapper.OrderPOMapper;
import com.xiaoming.backend.dao.mapper.OthserMapper;
import com.xiaoming.backend.dao.mapper.WastePOMapper;
import com.xiaoming.backend.entity.db.example.OrderPOExample;
import com.xiaoming.backend.entity.db.po.OrderInfoPO;
import com.xiaoming.backend.entity.db.po.OrderPO;
import com.xiaoming.backend.entity.db.po.TaskInfoPO;
import com.xiaoming.backend.entity.db.po.WastePO;
import com.xiaoming.backend.entity.orm.Order;
import com.xiaoming.backend.entity.orm.Task;
import com.xiaoming.backend.result.exception.AppErrorException;
import com.xiaoming.backend.result.exception.error.AppError;
import com.xiaoming.backend.util.DateUtil;
import com.xiaoming.backend.util.DeepCopyUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class OrderDao implements IOrderDao {
    private final  OrderPOMapper orderPOMapper;

    private final WastePOMapper wastePOMapper;

    private final  OthserMapper othserMapper;
    @Override
    public int insertOrder(OrderPO orderPO) {
        return orderPOMapper.insert(orderPO);
    }

    @Override
    public int getOrderId(OrderPO orderPO) {
        OrderPOExample e = new OrderPOExample();
        e.createCriteria().andOrderSqnoEqualTo(orderPO.getOrderSqno());
        List<OrderPO> orderPOS = orderPOMapper.selectByExample(e);
        return orderPOS.get(0).getOrderId();
    }

    @Override
    public List<Order> getOrderInfo(Long disposqlUserId, Long crtdUserId, String status, Long startFacilityId,Long endFacilityId,
                                    Date disposqlStartDate, Date disposqlEndDate, Date crtdStartDate, Date crtdEndDate) {
        List<OrderInfoPO> orderInfoPOS = othserMapper.getOrderInfo(disposqlUserId, crtdUserId, status, startFacilityId, endFacilityId,
                disposqlStartDate, disposqlEndDate, crtdStartDate, crtdEndDate);

        return orderInfoPOS.stream()
                .map(po -> DeepCopyUtil.copyByJackson(po, Order.class))
                .collect(Collectors.toList());
    }

    @Override
    public int uptWaste(Integer orderId, String status,Long disposqlUserId, String remark, String disposqlProgress, Long startFacilityId, Long endFacilityId, Long wasteType, Long disposalMethod) {
        OrderPO orderPO = orderPOMapper.selectByPrimaryKey(orderId);
        if (orderPO == null){
            throw new AppErrorException(AppError.FIND_ORDER_ERROR);
        }
        orderPO.setOrderStatus(status);
        orderPO.setDisposqlUserId(disposqlUserId);
        orderPO.setDisposqlDate(new Date());
        orderPO.setDisposqlRemark(remark);
        orderPO.setDisposqlProgress(disposqlProgress);
        if (startFacilityId != null) {
            orderPO.setOrderStartFacilityId(startFacilityId);
        }
        if (endFacilityId != null){
            orderPO.setOrderEndFacilityId(endFacilityId);
        }


        orderPOMapper.updateByPrimaryKey(orderPO);
        WastePO wastePO = wastePOMapper.selectByPrimaryKey(orderPO.getOrderWasteId());
        if (wasteType != null) {
            wastePO.setWasteType(wasteType);
        }
        if (disposalMethod != null) {
            wastePO.setDisposalMethod(disposalMethod);
        }
        wastePOMapper.updateByPrimaryKey(wastePO);
        return orderPOMapper.updateByPrimaryKey(orderPO);
    }

    @Override
    public String getMedicalOrderCount(Long userId) {
        OrderPOExample orderPOExample = new OrderPOExample();
        orderPOExample.createCriteria().andCrtdUserIdEqualTo(userId);
        long l = orderPOMapper.countByExample(orderPOExample);
        return String.valueOf(l);
    }

    @Override
    public String getMedicalOrderCount(Long userId, Date date) {
        OrderPOExample orderPOExample = new OrderPOExample();
        orderPOExample.createCriteria().andCrtdUserIdEqualTo(userId)
                .andCrtdDateGreaterThanOrEqualTo(DateUtil.removeTime(date));
        long l = orderPOMapper.countByExample(orderPOExample);
        return String.valueOf(l);
    }

    @Override
    public String getHandleOrderCount(Long userId) {
        OrderPOExample orderPOExample = new OrderPOExample();
        orderPOExample.createCriteria().andDisposqlUserIdEqualTo(userId);
        long l = orderPOMapper.countByExample(orderPOExample);
        return String.valueOf(l);
    }

    @Override
    public String getHandleOrderCount(Long userId, Date date) {
        OrderPOExample orderPOExample = new OrderPOExample();
        orderPOExample.createCriteria().andDisposqlUserIdEqualTo(userId)
                .andDisposqlDateGreaterThanOrEqualTo(DateUtil.removeTime(date));
        long l = orderPOMapper.countByExample(orderPOExample);
        return String.valueOf(l);
    }

    @Override
    public Long getOrderAdminCount(Long facilityId) {
        OrderPOExample orderPOExample = new OrderPOExample();
        orderPOExample.createCriteria().andOrderStartFacilityIdEqualTo(facilityId);
        long l1 = orderPOMapper.countByExample(orderPOExample);

        orderPOExample.clear();
        orderPOExample.createCriteria().andOrderEndFacilityIdEqualTo(facilityId);
        long l2 = orderPOMapper.countByExample(orderPOExample);

        return l1 + l2;
    }

    @Override
    public Long getOrderAdminCount(Long facilityId, Date date) {
        OrderPOExample orderPOExample = new OrderPOExample();
        orderPOExample.createCriteria().andOrderStartFacilityIdEqualTo(facilityId)
                .andDisposqlDateGreaterThanOrEqualTo(DateUtil.removeTime(date));;
        long l1 = orderPOMapper.countByExample(orderPOExample);

        orderPOExample.clear();
        orderPOExample.createCriteria().andOrderEndFacilityIdEqualTo(facilityId)
                .andDisposqlDateGreaterThanOrEqualTo(DateUtil.removeTime(date));;
        long l2 = orderPOMapper.countByExample(orderPOExample);

        return l1 + l2;
    }

}
